extends Control

var world_scene : PackedScene
var selected_map_name : String

onready var world = preload("res://World.tscn")

func _ready():
	var maps = update_map_list()
	for m in maps:
		$"%MapList".add_item(m)

func update_map_list() -> Array:
	var dir := Directory.new()
	var arr := []
	var path := "res://Maps"
	if dir.open(path) == OK:
		dir.list_dir_begin()
		var file_name = dir.get_next()
		while file_name != "":
			if file_name != "." or file_name != "..":
				arr.append(file_name.split(".")[0])
				file_name = dir.get_next()
	return arr


func _on_PlayButton_pressed():
	var world_instance : PanaWorld = world.instance()
	world_instance.load_map_from_name(selected_map_name)
	var root = get_node("/root")
	root.get_node("MapViewer").queue_free()
	root.add_child(world_instance)

func _on_MapList_select_map(smn):
	selected_map_name = smn
